/**
 * 
 */
package com.alkandros.linesmash.ui;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alkandros.linesmash.R;
import com.alkandros.linesmash.adapter.RestaurantAdapter;
import com.alkandros.linesmash.adapter.VenuesAdapter;
import com.alkandros.linesmash.location.ILocationCallBack;
import com.alkandros.linesmash.manager.APIManager;
import com.alkandros.linesmash.manager.AlertManager;
import com.alkandros.linesmash.models.Location;
import com.alkandros.linesmash.models.Restaurant;

/**
 * @author Jiju Induchoodan
 *
 */
public class RestaurantSelection extends BaseActivity implements ILocationCallBack {

	private FetchRestaturant fetchRestaurants;
	private ListView list;
	private Intent intent;
	private RelativeLayout relativeLocation;
	private LinearLayout linearCategory;
	private RestaurantAdapter resAdapter;
	ArrayList<Restaurant> restaurantArray;
	private Restaurant restaurantModel;
	private TextView txtRightTab;
	private TextView txtLeftTab;
	private Bundle extras;
	private Location locationModel;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dining_locations);
		extras = getIntent().getExtras();
		if(extras!=null)
		{
			locationModel = (Location) extras.get("locationModel");
		}
		initialiseUI();
	}
	
	private void initialiseUI()
	{
		setHeading(locationModel.getTitle());
		setLeftButton();
		setRightButton();
		list    			= (ListView)findViewById(R.id.list);
	    txtLeftTab  		= (TextView)findViewById(R.id.txt_leftTab);
	    txtRightTab  		= (TextView)findViewById(R.id.txt_RightTab);
	    linearCategory		= (LinearLayout)findViewById(R.id.linear_category);
	    relativeLocation	= (RelativeLayout)findViewById(R.id.relative_location);
	    relativeLocation.setVisibility(View.GONE);
	    restaurantArray     = new ArrayList<Restaurant>();
	    
	    txtRightTab.setText(R.string.open_now);
	    txtLeftTab.setText(R.string.popularity);
	    
	    fetchRestaurants = new FetchRestaturant();
	    fetchRestaurants.execute();
	 
     	 list.setOnItemClickListener(new OnItemClickListener() {
	 			@Override
	 			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
	 					long arg3) {
	 				intent=  new Intent(RestaurantSelection.this,RestaurantMenu.class);
	 				intent.putExtra("model", (Restaurant)list.getItemAtPosition(arg2));
	 				startActivity(intent);
	 			}
	 		});
	}
	class FetchRestaturant extends AsyncTask<Void, Void, Void>
	{
		private APIManager manager;
		private boolean isException=false;
		private String response;
		private String [] names= {"",""};
		private String [] values= {"",""};
		
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			progress.dismiss();
			if(isException)
			{
				AlertManager.shorttoastMessage(RestaurantSelection.this, getString(R.string.common_error_message));
			}
			else
			{
				if(response.contains(ERROR_CODE))
				{
					AlertManager.shorttoastMessage(RestaurantSelection.this, getString(R.string.common_error_message));
				}
				else
				{
					try
					{
						JSONObject obj = new JSONObject(response);
						JSONArray array = obj.getJSONArray(DATA);
						for (int i = 0; i < array.length(); i++) 
						{
							Restaurant model = new Restaurant();
							JSONObject postObject = array.getJSONObject(i).getJSONObject(RESTAURANT);
							model.setTitle(postObject.getString(NAME));
							model.setId(postObject.getString(ID));
							model.setRating( Float.parseFloat(postObject.getString(RATING)));
							model.setImage(postObject.getString(IMAGE));
							restaurantArray.add(model);
						}
							resAdapter = new RestaurantAdapter(context,restaurantArray);
							list.setAdapter(resAdapter); 
					}
					catch(Exception e)
					{
						Log.e("", "Exception"+e.getMessage());
						AlertManager.shorttoastMessage(RestaurantSelection.this, getString(R.string.common_error_message));
					}
					
				}
			}
		}
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			progress.show();
			manager = new APIManager(URL_RESTAURANTS);
		}

		@Override
		protected Void doInBackground(Void... params) {
			try 
			{
				response= manager.commonPostData(URL_RESTAURANTS+locationModel.getId(),names,values);
			}
			catch (Exception e)
			{
				e.printStackTrace();
				isException=true;
			}
			return null;
		}
		
	}
	/* (non-Javadoc)
	 * @see com.alkandros.linesmash.location.ILocationCallBack#setResult(java.lang.String)
	 */
	@Override
	public void setResult(String address) {
		// TODO Auto-generated method stub
		
	}
}